package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.config.auth.SecurityUtil;
import com.example.config.exception.GlobalExcetion;
import com.example.domain.BusContent;
import com.example.domain.BusNote;
import com.example.domain.BusType;
import com.example.domain.dto.BusNoteDto;
import com.example.config.redis.CacheService;
import com.example.service.BusContentService;
import com.example.service.BusNoteService;
import com.example.mapper.BusNoteMapper;
import com.example.service.BusTypeService;
import com.example.domain.vo.BusNoteVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author CETC
* @description 针对表【bus_note】的数据库操作Service实现
* @createDate 2025-10-21 11:42:46
*/
@Service
public class BusNoteServiceImpl extends ServiceImpl<BusNoteMapper, BusNote>
    implements BusNoteService{

    private static final String BUSNOTE_CACHE_NAME = "bus_note";

    Logger logger=Logger.getLogger(BusNoteServiceImpl.class.getName());

    @Autowired
    private BusTypeService busTypeService;

    @Autowired
    private BusContentService busContentService;

    @Autowired
    private CacheService cacheService;

    @Override
    public BusNote saveOrUpdateBusNote(BusNoteVo busNote) throws GlobalExcetion {
        String nodeId=SecurityUtil.getCurrentUserNodeId();
        String userId=SecurityUtil.getUserId();
        BusType type=busTypeService.getBusType(busNote.getTitleTypeId(),userId);
        if(type==null){
            throw new GlobalExcetion(1,"文章类型不存在");
        }
        if(busNote.getId()==null){
            BusNote note=new BusNote();
            BeanUtils.copyProperties(busNote,note);
            note.setIsDeleted(0);
            note.setDsNo(nodeId);
            note.setUserId(userId);
            this.save(note);
            BusContent busContent=new BusContent();
            busContent.setContentId(note.getId());
            busContent.setContentText(busNote.getContent());
            busContent.setIsEnable(true);
            busContent.setContentVersion(new Date());
            busContentService.save(busContent);
            return note;
        }else {
            BusNote note=this.getById(busNote.getId());
            if(note!=null){
                note.setTitleName(busNote.getTitleName());
                note.setTitleTag(busNote.getTitleTag());
                note.setTitleTypeId(busNote.getTitleTypeId());
                //禁用其他文章
                busContentService.disableBusContentListByContentId(note.getId());
                BusContent busContent=new BusContent();
                busContent.setContentId(note.getId());
                busContent.setContentText(busNote.getContent());
                busContent.setIsEnable(true);
                busContent.setContentVersion(new Date());
                busContentService.save(busContent);
                //更新缓存
                cacheService.evict(BUSNOTE_CACHE_NAME, busNote.getId());
                this.updateById(note);
                return note;
            }
            return null;
        }
    }


    @Override
    public IPage<BusNote> pageBusNote(String userId, Integer pageNo, Integer pageSize, String keyword) {
        LambdaQueryWrapper<BusNote> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BusNote::getUserId,userId);
        if(StringUtils.hasText(keyword)){
            lambdaQueryWrapper.like(BusNote::getTitleName,keyword);
        }
        lambdaQueryWrapper.orderByDesc(BusNote::getCreateTime);
        IPage<BusNote> page=this.page(new Page(pageNo, pageSize), lambdaQueryWrapper);
        return page;
    }

    @Override
    @Cacheable(value = BUSNOTE_CACHE_NAME, key = "#id")
    public BusNote getBusNoteCache(String id){
        return this.getById(id);
    }

    @CacheEvict(key = "#id")
    public void evictBusNoteCache(String id) {
        // 这个方法专门用于清除缓存，不需要业务逻辑
        logger.log(Level.INFO,"清除文章数据缓存id:"+id);
    }

    @Override
    public BusNoteDto getBusNoteDtoCache(String id) {
        BusNote busNote=null;
        if(cacheService.containsKey(BUSNOTE_CACHE_NAME,id)){
            busNote=cacheService.get(BUSNOTE_CACHE_NAME,id,BusNote.class);
            if(busNote==null){
               return null;
            }
        }else {
            busNote=this.getById(id);
            cacheService.put(BUSNOTE_CACHE_NAME,id,busNote);
            if(busNote==null){
                return null;
            }
        }
        BusNoteDto busNoteDto=new BusNoteDto();
        BeanUtils.copyProperties(busNote,busNoteDto);
        BusType busType=busTypeService.getBusType(busNote.getTitleTypeId(),busNote.getUserId());
        if(busType!=null){
            busNoteDto.setNoteTypeName(busType.getTypeName());
        }
        BusContent busContent=busContentService.getContent(busNote.getId());
        if(busContent!=null){
            busNoteDto.setNoteContent(busContent.getContentText());
        }
        return busNoteDto;
    }
}




